/**
 * 
 * @Project HRL Simulation
 * @Date 20.06.2011
 * @Author Aebi Pascal, Kocher Marc, Sriranjan George
 * @School BFH-TI Biel, Projekt 1
 *
 */

package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;


public class Prop {

	public final static String LIFT_NUMBER = "LIFT_NUMBER";
	public final static String RACK_LENGTH = "RACK_LENGTH";
	public final static String RACK_HEIGHT = "RACK_HEIGHT";
	public final static String LIFT_INIT_POSITION = "LIFT_INIT_POSITION";
	public final static String CONVEYOR_SPEED = "CONVEYOR_SPEED";
	public final static String LIFT_SPEED = "LIFT_SPEED";
	public final static String SIMULATION_SPEED = "SIMULATION_SPEED";
	public final static String STRATEGY = "STRATEGY";
	public final static String RANDOM_LOADS = "RANDOM_LOADS";
	public final static String RANDOM_LOAD_NUMBER = "RANDOM_LOAD_NUMBER";
	public final static String IN_LOADS = "IN_LOADS";
	public final static String OUT_LOADS = "OUT_LOADS";
	public final static String STORE_LOADS = "STORE_LOADS";
	public static Properties p;


	/**
	 * Prop constructor
	 */
	public Prop(){
		p = new Properties();
		p.setProperty(Prop.LIFT_NUMBER, "1");
		p.setProperty(Prop.RACK_LENGTH, "1");
		p.setProperty(Prop.RACK_HEIGHT, "1");
		p.setProperty(Prop.LIFT_INIT_POSITION, "1");
		p.setProperty(Prop.CONVEYOR_SPEED, "1");
		p.setProperty(Prop.LIFT_SPEED, "1");
		p.setProperty(Prop.SIMULATION_SPEED, "1");
		p.setProperty(Prop.STRATEGY, "1");
		p.setProperty(Prop.RANDOM_LOADS, "1");
		p.setProperty(Prop.RANDOM_LOAD_NUMBER, "1");
		p.setProperty(Prop.IN_LOADS, "0");
		p.setProperty(Prop.OUT_LOADS, "0");
		p.setProperty(Prop.STORE_LOADS, "0");
	}
	
	
	/**
	 * Gets integer value of given string if the conditions were accepted
	 * 
	 * @param key
	 * @return check value
	 */
	public static int getInt(String key){
		return checkValue(key);
	}
	
	
	/**
	 * Set a property
	 * 
	 * @param key
	 * @param value
	 */
	public void setProperty(String key, String value) {
		p.setProperty(key, value);
	}

	
	/**
	 * Load all properties from a given file
	 */
	public static void load(File file) {
		try {
			FileInputStream in = new FileInputStream(file);
			p.load(in);
			in.close();
			//TODO check all properties for correctness
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("load error");
			e.printStackTrace();
		}

	}
	
	
	/**
	 * Store all properties into a given file
	 */
	public static void store(File file) {
		try {
			FileOutputStream out = new FileOutputStream(file);
			p.store(out, "Store all settings of \"HRL Simulation\" in this file");
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	/**
	 * Check all values before loading and saving 
	 */
	private static int checkValue(String key){
		String sValue = p.getProperty(key);
		if(sValue.matches("[0-9-]+")){
			return Integer.valueOf(p.getProperty(key)).intValue();
		}
		if(key.equals(Prop.IN_LOADS) || key.equals(Prop.OUT_LOADS) || key.equals(Prop.STORE_LOADS)){
			return 0;
		}
		return 1;
	}
	
}
